package com.flying.cms.service;

import java.util.Date;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.util.Codes;
import com.flying.common.util.StringUtils;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.context.ServiceContext;
import com.flying.framework.data.Data;
import com.flying.framework.exception.ObjectNotFoundException;
import com.flying.framework.security.Principal;
import com.flying.framework.service.AbstractService;

@Service("Comment")
public class CommentService  extends AbstractService{
	
	private ContentService contentService;
	
	public void setContentService(ContentService contentService) {
		this.contentService = contentService;
	}

	@MethodInfo("创建评论")
	@DaoCreate(entity="t_comment")
	public Data create(
			@Param(value="content_id",         required=true,	desc="主题ID") int content_id,
			@Param(value="category_id",         required=false,	desc="分类ID") int category_id,
			@Param(value="title",         required=false,	desc="标题") String title,
			@Param(value="content",         required=false,	desc="内容") String content,
			@Param(value="status",         required=false,	desc="状态(0：待审；1：已审通过；2：已审不通过)") int status,
			@Param(value="del_flag",         required=false,	desc="删除标志") int del_flag,
			@Param(value="creator",         required=false,	desc="创建人") String creator,
			@Param(value="create_date",         required=false,	desc="创建时间") Date create_date
		) throws Exception {
		Data c = this.contentService.findById(new Data("content_id", content_id));
		if(StringUtils.isEmpty(c.getString("title"))) {
			throw new ObjectNotFoundException(String.valueOf(content_id), "主题不存在");
		}
		return new Data(
				"category_id", c.getString("category_id"), 
				"status", 1,
				"del_flag", 0,
				"create_date", new Date()
			);
	}

	@MethodInfo("审核评论")
	@DaoUpdate(entity="t_comment")
	public Data auditComment(
			@Param(value="comment_id",         required=true,	desc="评论ID") int comment_id,
			@Param(value="status",         required=false,	desc="状态(0：待审；1：已审通过；2：已审不通过)") int status,
			@Param(value="updator",         required=false,	desc="修改人") String updator,
			@Param(value="update_date",         required=false,	desc="修改时间") Date update_date
		) throws Exception {
		Principal p = ServiceContext.getContext().getPrincipal();
		return new Data(
				"updator", p == null? null: p.getName(),
				"update_date", new Date()
			);
	}

	@MethodInfo("审核评论通过")
	public Data auditForOK(
			@Param(value="comment_id",         required=true,	desc="评论ID") int comment_id
		) throws Exception {
		return this.auditComment(comment_id, 1, null, null);
	}

	@MethodInfo("审核评论不通过")
	public Data auditForNOK(
			@Param(value="comment_id",         required=true,	desc="评论ID") int comment_id
		) throws Exception {
		return this.auditComment(comment_id, 2, null, null);
	}

	@MethodInfo("删除评论")
	@DaoUpdate(entity="t_comment")
	public Data delete(
			@Param(value="comment_id",         required=true,	desc="评论ID") int comment_id,
			@Param(value="del_flag",         required=false,	desc="删除标志") int del_flag,
			@Param(value="updator",         required=false,	desc="修改人") String updator,
			@Param(value="update_date",         required=false,	desc="修改时间") Date update_date
		) throws Exception {
		Principal p = ServiceContext.getContext().getPrincipal();
		return new Data(
				"del_flag", 1,
				"updator", p == null? null: p.getName(),
				"update_date", new Date()
			);
	}

	@MethodInfo("删除多条评论")
	public Data deleteComments(
			@Param(value="comment_id",         required=true,	desc="评论ID") int[] comment_ids
		) throws Exception {
		for(int id: comment_ids) {
			this.delete(id, 1, null, null);
		}
		return new Data(Codes.CODE, Codes.SUCCESS);
	}

	@MethodInfo("查询主题评论")
	@DaoQuery(entity="t_comment", pageable=true, wsql="del_flag=0", osql="order by create_date desc ")
	public Data findByContentId(
			@Param(value="content_id",         required=true,	desc="主题ID") int content_id,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows
		) {
		return null;
	}

	@MethodInfo("查询审批通过的评论")
	@DaoQuery(entity="t_comment", pageable=true, wsql="del_flag=0 and status=1", osql="order by create_date desc ")
	public Data findComments(
			@Param(value="content_id",         required=true,	desc="主题ID") int content_id,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows
		) {
		return new Data(
				"page", 0,
				"rows", 100000
				);
	}
}
